/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sysdrive.controller.Fornecedor;

import br.com.sysdrive.DAO.FornecedorDAOImpl;
import br.com.sysdrive.DAO.GenericDAO;
import br.com.sysdrive.DAO.UsuarioDAOImpl;
import br.com.sysdrive.model.Fornecedor;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author WillianFrias
 */
public class AlterarFornecedor extends HttpServlet {

    public static Date converterData(String data) throws ParseException {
        SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
        if (data == null || data.trim().equals("")) {
            return null;
        } else {
            Date date = fmt.parse(data);
            return date;
        }
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {

        Fornecedor fornecedor = new Fornecedor();
        fornecedor.setIdUsuario(Integer.parseInt(request.getParameter("idusuario")));
        fornecedor.setNomeUsuario(request.getParameter("nomeusuario"));
        fornecedor.setDataNascimentoUsuario(converterData(request.getParameter("datanascimentousuario")));
        fornecedor.setSexoUsuario(request.getParameter("sexousuario"));
        fornecedor.setRgIeUsuario(request.getParameter("rgieusuario"));
        fornecedor.setCpfCnpjUsuario(request.getParameter("cpfcnpjusuario"));
        fornecedor.setRuaEnderecoUsuario(request.getParameter("ruaenderecousuario"));
        fornecedor.setNumeroEnderecoUsuario(Integer.parseInt(request.getParameter("numeroenderecousuario")));
        fornecedor.setBairroUsuario(request.getParameter("bairrousuario"));
        fornecedor.setComplementoUsuario(request.getParameter("complementousuario"));
        fornecedor.setCidadeUsuario(request.getParameter("cidadeusuario"));
        fornecedor.setCepUsuario(request.getParameter("cepusuario"));
        fornecedor.setUfUsuario(request.getParameter("ufusuario"));
        fornecedor.setEmailUsuario(request.getParameter("emailusuario"));
        fornecedor.setTelefoneUsuario(request.getParameter("telefoneusuario"));
        fornecedor.setCelularUsuario(request.getParameter("celularusuario"));
        fornecedor.setDataCadastroUsuario(converterData(request.getParameter("datacadastrousuario")));
        fornecedor.setStatusUsuario(request.getParameter("statususuario"));
        fornecedor.setLoginUsuario(request.getParameter("loginusuario"));
        fornecedor.setSenhaUsuario(request.getParameter("senhausuario"));
        fornecedor.setNomeFantasiaFornecedor(request.getParameter("nomefantasiafornecedor"));
        fornecedor.setRazaoSocialFornecedor(request.getParameter("razaosocialfornecedor"));
        fornecedor.setRamoNegocioFornecedor(request.getParameter("ramonegociofornecedor"));

        String msg = "";
        try {
            GenericDAO daoFornecedor = new FornecedorDAOImpl();
            if (daoFornecedor.alterar(fornecedor)) {
                UsuarioDAOImpl daoUsuario = new UsuarioDAOImpl();
                if (daoUsuario.alterar(fornecedor)) {
                    msg = "Fornecedor alterado com sucesso!";
                } else {
                    msg = "Problemas ao realizar alteração! Fornecedor não Alterado!";
                }
            }
            request.setAttribute("mensagem", msg);
            request.getRequestDispatcher("ListarFornecedor").forward(request, response);
        } catch (Exception ex) {
            System.out.println("Problemas no Servlet AlterarFornecedor: " + ex.getMessage());
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(AlterarFornecedor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(AlterarFornecedor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
